//The MIT License (MIT)
//
//Copyright (c) 2018 dote
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of
//this software and associated documentation files (the "Software"), to deal in
//the Software without restriction, including without limitation the rights to
//use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
//the Software, and to permit persons to whom the Software is furnished to do so,
//subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
//FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
//COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
//IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
//CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.github.dote.mio.filter.ext;
 

import java.io.Serializable;

public class PageRequest implements Pageable, Serializable {
    private static final long serialVersionUID = 8280485938848398236L;
    private final int page;
    private final int size;
    private final Sort sort;

    public PageRequest(int page, int size) {
        this(page, size, (Sort)null);
    }

    public PageRequest(int page, int size, Sort.Direction direction, String property) {
        this(page, size, new Sort(direction, property));
    }

    public PageRequest(int page, int size, Sort sort) {
        if (page < 0) {
            throw new IllegalArgumentException("Page index must not be less than zero!");
        } else if (size < 0) {
            throw new IllegalArgumentException("Page size must not be less than zero!");
        } else {
            this.page = page;
            this.size = size;
            this.sort = sort;
        }
    }

    public int getPageSize() {
        return this.size;
    }

    public int getPageNumber() {
        return this.page;
    }

    public int getOffset() {
        return this.page * this.size;
    }

    public Sort getSort() {
        return this.sort;
    }

    public boolean hasPrevious() {
        return this.page > 0;
    }

    public Pageable next() {
        return new PageRequest(this.page + 1, this.size, this.sort);
    }

    public Pageable previousOrFirst() {
        return this.hasPrevious() ? new PageRequest(this.page - 1, this.size, this.sort) : this;
    }

    public Pageable first() {
        return new PageRequest(0, this.size, this.sort);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (!(obj instanceof PageRequest)) {
            return false;
        } else {
            PageRequest that = (PageRequest)obj;
            boolean pageEqual = this.page == that.page;
            boolean sizeEqual = this.size == that.size;
            boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort);
            return pageEqual && sizeEqual && sortEqual;
        }
    }

    public int hashCode() {
        int result1 = 17;
        int result = 31 * result1 + this.page;
        result = 31 * result + this.size;
        result = 31 * result + (null == this.sort ? 0 : this.sort.hashCode());
        return result;
    }

    public String toString() {
        return String.format("Page request [number: %d, size %d, sort: %s]", this.page, this.size, this.sort == null ? null : this.sort.toString());
    }
}

